; Script generated with the Venis Install Wizard

; Define your application name
!define APPNAME "Ogre VC10 AppWizard"
!define APPNAMEANDVERSION "Ogre VC10 AppWizard  1.7.3"

; Main Install settings
Name "${APPNAMEANDVERSION}"
InstallDir "$PROGRAMFILES\Ogre VC10 AppWizard"
InstallDirRegKey HKLM "Software\${APPNAME}" ""
OutFile "Ogre_VC10_AppWizard_1.7.3.exe"

; Modern interface settings
!include "MUI.nsh"

!define MUI_ABORTWARNING

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

; Set languages (first is default language)
!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_RESERVEFILE_LANGDLL

Section "Ogre VC10 AppWizard" Section1

	; Set Section properties
	SetOverwrite on

	; Set Section Files and Shortcuts
	SetOutPath "$INSTDIR\"
  File "Ogre_VC10_AppWizard_readme.txt"
  File "Files\default.props"
	File "Files\default.vcxproj"
	File "Files\OgreAppWizard100.ico"
	File "Files\OgreAppWizard100.vsdir"
	File "Files\OgreAppWizard100.vsz"
	File "Files\vcupgradedefault.vcxproj"
	SetOutPath "$INSTDIR\1033\"
	File "Files\1033\NewStyles.css"
	File "Files\1033\styles.css"
	SetOutPath "$INSTDIR\html\1033\"
	File "Files\html\1033\AppSettings.htm"
	File "Files\html\1033\default.htm"
	SetOutPath "$INSTDIR\Images\"
	File "Files\Images\DottedHori.gif"
	File "Files\Images\DottedVert.gif"
	File "Files\Images\OgreAppWizard80.png"
	File "Files\Images\OgreAppWizard80_Background.png"
	File "Files\Images\spacer.gif"
	SetOutPath "$INSTDIR\scripts\1033\"
	File "Files\scripts\1033\default.js"
	SetOutPath "$INSTDIR\templates\1033\"
	File "Files\templates\1033\Templates.inf"
	SetOutPath "$INSTDIR\templates\1033\docs\"
	File "Files\templates\1033\docs\readme.txt"
	SetOutPath "$INSTDIR\templates\1033\include\"
	File "Files\templates\1033\include\BaseApplication.h"
	File "Files\templates\1033\include\root.h"
	File "Files\templates\1033\include\root_mini.h"
	File "Files\templates\1033\include\root_one.h"
	SetOutPath "$INSTDIR\templates\1033\res\"
	File "Files\templates\1033\res\resource.h"
	File "Files\templates\1033\res\root.ico"
	File "Files\templates\1033\res\root.rc"
	SetOutPath "$INSTDIR\templates\1033\src\"
	File "Files\templates\1033\src\BaseApplication.cpp"
	File "Files\templates\1033\src\root.cpp"
	File "Files\templates\1033\src\root_mini.cpp"
	File "Files\templates\1033\src\root_one.cpp"
	CreateDirectory "$SMPROGRAMS\Ogre VC10 AppWizard"
	CreateShortCut "$SMPROGRAMS\Ogre VC10 AppWizard\Uninstall.lnk" "$INSTDIR\uninstall.exe"
	CreateShortCut "$SMPROGRAMS\Ogre VC10 AppWizard\Readme.lnk" "$INSTDIR\Ogre_VC10_AppWizard_readme.txt"

	Push @absolutepath@                        #text to be replaced
  Push $INSTDIR					                     #replace with
  Push all                                   #replace all occurrences
  Push all                                   #replace all occurrences
  Push $INSTDIR\OgreAppWizard100.vsz         #file to replace in
  Call AdvReplaceInFile                      #call find and replace function
SectionEnd

Section -FinishSection

	; Set Section properties
	SetOverwrite on

	WriteRegStr HKLM "Software\${APPNAME}" "" "$INSTDIR"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayName" "${APPNAME}"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "UninstallString" "$INSTDIR\uninstall.exe"
	WriteUninstaller "$INSTDIR\uninstall.exe"

  ClearErrors
	ReadRegStr $0  HKLM "SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Setup\VC" 'ProductDir'
	StrCmp $0 "" tryAgain
	Goto RegOK

tryAgain:
	ClearErrors
	ReadRegStr $0  HKLM "SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VC" 'ProductDir'
	StrCmp $0 "" ohNo
	Goto RegOK

ohNo:
	MessageBox MB_OK "Cannot find Visual Studio... \nDo a manual install"
	Goto End

RegOK:
	CopyFiles "$INSTDIR\OgreAppWizard100.vsz" "$0VCProjects\OgreAppWizard100.vsz"
	CopyFiles "$INSTDIR\OgreAppWizard100.ico" "$0VCProjects\OgreAppWizard100.ico"
	CopyFiles "$INSTDIR\OgreAppWizard100.vsdir" "$0VCProjects\OgreAppWizard100.vsdir"
End:
  ; View readme
  ExecShell "open" '"$INSTDIR\Ogre_VC10_AppWizard_readme.txt"'
SectionEnd

; Modern install component descriptions
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
	!insertmacro MUI_DESCRIPTION_TEXT ${Section1} ""
!insertmacro MUI_FUNCTION_DESCRIPTION_END

;Uninstall section
Section Uninstall

	;Remove from registry...
	DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
	DeleteRegKey HKLM "SOFTWARE\${APPNAME}"

	; Delete self
	Delete "$INSTDIR\uninstall.exe"

	; Delete Shortcuts
	Delete "$SMPROGRAMS\Ogre VC10 AppWizard\Uninstall.lnk"
	Delete "$SMPROGRAMS\Ogre VC10 AppWizard\Readme.lnk"

  ; Clean up Ogre VC10 AppWizard
  Delete "$INSTDIR\Ogre_VC10_AppWizard_readme.txt"
	Delete "$INSTDIR\default.props"
	Delete "$INSTDIR\default.vcxproj"
	Delete "$INSTDIR\OgreAppWizard100.ico"
	Delete "$INSTDIR\OgreAppWizard100.vsdir"
	Delete "$INSTDIR\OgreAppWizard100.vsz"
	Delete "$INSTDIR\vcupgradedefault.vcxproj"
	Delete "$INSTDIR\1033\NewStyles.css"
	Delete "$INSTDIR\1033\styles.css"
	Delete "$INSTDIR\html\1033\AppSettings.htm"
	Delete "$INSTDIR\html\1033\default.htm"
	Delete "$INSTDIR\Images\DottedHori.gif"
	Delete "$INSTDIR\Images\DottedVert.gif"
	Delete "$INSTDIR\Images\OgreAppWizard80.png"
	Delete "$INSTDIR\Images\OgreAppWizard80_Background.png"
	Delete "$INSTDIR\Images\spacer.gif"
	Delete "$INSTDIR\scripts\1033\default.js"
	Delete "$INSTDIR\templates\1033\Templates.inf"
	Delete "$INSTDIR\templates\1033\docs\readme.txt"
	Delete "$INSTDIR\templates\1033\include\BaseApplication.h"
	Delete "$INSTDIR\templates\1033\include\root.h"
	Delete "$INSTDIR\templates\1033\include\root_mini.h"
	Delete "$INSTDIR\templates\1033\include\root_one.h"
	Delete "$INSTDIR\templates\1033\res\resource.h"
	Delete "$INSTDIR\templates\1033\res\root.ico"
	Delete "$INSTDIR\templates\1033\res\root.rc"
	Delete "$INSTDIR\templates\1033\src\BaseApplication.cpp"
	Delete "$INSTDIR\templates\1033\src\root.cpp"
	Delete "$INSTDIR\templates\1033\src\root_mini.cpp"
	Delete "$INSTDIR\templates\1033\src\root_one.cpp"

	; Remove remaining directories
	RMDir "$SMPROGRAMS\Ogre VC10 AppWizard"
	RMDir "$INSTDIR\templates\1033\src\"
	RMDir "$INSTDIR\templates\1033\res\"
	RMDir "$INSTDIR\templates\1033\include\"
	RMDir "$INSTDIR\templates\1033\docs\"
	RMDir "$INSTDIR\templates\1033\"
	RMDir "$INSTDIR\templates\"
	RMDir "$INSTDIR\scripts\1033\"
	RMDir "$INSTDIR\scripts\"
	RMDir "$INSTDIR\Images\"
	RMDir "$INSTDIR\html\1033\"
	RMDir "$INSTDIR\html\"
	RMDir "$INSTDIR\1033\"
	RMDir "$INSTDIR\"

  ; Get rid of installed Ogre AppWizard files in the VC dir
  ClearErrors
	ReadRegStr $0  HKLM "SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Setup\VC" 'ProductDir'
	StrCmp $0 "" tryAgain
	Goto RegOK

tryAgain:
	ClearErrors
	ReadRegStr $0  HKLM "SOFTWARE\Microsoft\VisualStudio\10.0\Setup\VC" 'ProductDir'
	StrCmp $0 "" ohNo
	Goto RegOK

ohNo:
	MessageBox MB_OK "Cannot find Visual Studio.. \nRemove files manually"
	Goto End

RegOK:
		Delete "$0VCProjects\OgreAppWizard100.vsz"
		Delete "$0VCProjects\OgreAppWizard100.ico"
		Delete "$0VCProjects\OgreAppWizard100.vsdir"
End:

SectionEnd

Function AdvReplaceInFile
 
         ; call stack frame:
         ;   0 (Top Of Stack) file to replace in
         ;   1 number to replace after (all is valid)
         ;   2 replace and onwards (all is valid)
         ;   3 replace with
         ;   4 to replace
 
         ; save work registers and retrieve function parameters
         Exch $0 ;file to replace in
         Exch 4
         Exch $4 ;to replace
         Exch
         Exch $1 ;number to replace after
         Exch 3
         Exch $3 ;replace with
         Exch 2
         Exch $2 ;replace and onwards
         Exch 2
         Exch 
         Push $5 ;minus count
         Push $6 ;universal
         Push $7 ;end string
         Push $8 ;left string
         Push $9 ;right string
         Push $R0 ;file1
         Push $R1 ;file2
         Push $R2 ;read
         Push $R3 ;universal
         Push $R4 ;count (onwards)
         Push $R5 ;count (after)
         Push $R6 ;temp file name
         GetTempFileName $R6
         FileOpen $R1 $0 r ;file to search in
         FileOpen $R0 $R6 w ;temp file
                  StrLen $R3 $4
                  StrCpy $R4 -1
                  StrCpy $R5 -1
        loop_read:
         ClearErrors
         FileRead $R1 $R2 ;read line
         IfErrors exit
         StrCpy $5 0
         StrCpy $7 $R2
 
        loop_filter:
         IntOp $5 $5 - 1
         StrCpy $6 $7 $R3 $5 ;search
         StrCmp $6 "" file_write2
         StrCmp $6 $4 0 loop_filter
 
         StrCpy $8 $7 $5 ;left part
         IntOp $6 $5 + $R3
         StrCpy $9 $7 "" $6 ;right part
         StrCpy $7 $8$3$9 ;re-join
 
         IntOp $R4 $R4 + 1
         StrCmp $2 all file_write1
         StrCmp $R4 $2 0 file_write2
         IntOp $R4 $R4 - 1
 
         IntOp $R5 $R5 + 1
         StrCmp $1 all file_write1
         StrCmp $R5 $1 0 file_write1
         IntOp $R5 $R5 - 1
         Goto file_write2
 
        file_write1:
         FileWrite $R0 $7 ;write modified line
         Goto loop_read
 
        file_write2:
         FileWrite $R0 $R2 ;write unmodified line
         Goto loop_read
 
        exit:
         FileClose $R0
         FileClose $R1
 
         SetDetailsPrint none
         Delete $0
         Rename $R6 $0
         Delete $R6
         SetDetailsPrint both
 
         Pop $R6
         Pop $R5
         Pop $R4
         Pop $R3
         Pop $R2
         Pop $R1
         Pop $R0
         Pop $9
         Pop $8
         Pop $7
         Pop $6
         Pop $5
         Pop $4
         Pop $3
         Pop $2
         Pop $1
         Pop $0
FunctionEnd

; eof